package com.xbongbong.pro.statistic.config;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.pro.service.toolbox.statistic.help.ThroughProService;
import com.xbongbong.pro.statistic.service.ChartResultProService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 图表服务配置
 *
 * @author LiZhikang
 * @date 2022/11/1 16:37
 */

@Configuration
public class ChartServiceConfig implements ApplicationContextAware {

    private Map<String, ChartResultProService> chartResultProMap;
    private Map<String, ThroughProService> throughProMap;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.chartResultProMap = applicationContext.getBeansOfType(ChartResultProService.class);
        this.throughProMap = applicationContext.getBeansOfType(ThroughProService.class);
    }

    /**
     * 图表结果增强
     *
     * @return
     */
    @Bean
    public Map<Integer, ChartResultProService> chartResultProMap() {
        Map<Integer, ChartResultProService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.chartResultProMap.forEach((key, item) -> {
            item.businessType().forEach(businessType -> resultMap.put(businessType, item));
        });
        return resultMap;
    }

    /**
     * 穿透增强
     *
     * @return
     */
    @Bean
    public Map<Integer, ThroughProService> throughProMap() {
        Map<Integer, ThroughProService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.throughProMap.forEach((key, item) -> {
            item.businessType().forEach(businessType -> resultMap.put(businessType, item));
        });
        return resultMap;
    }

}
